package com.lcredu.springbootusersys.controller;

import com.lcredu.springbootusersys.exception.AccessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.HandlerMethod;

/**
 * @author 黎长容
 * @date 2025-08-16 21:24
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 1. 处理算数异常和空指针异常
     */
    @ExceptionHandler({ArithmeticException.class, NullPointerException.class, AccessException.class})
    public String handleArithmeticException(Exception e, Model model, HandlerMethod handlerMethod) {
        log.info("获取到的异常信息为" + e.getMessage());
        model.addAttribute("msg", e.getMessage());
        log.info("被捕获到的异常=" + handlerMethod.getMethod());
        return "/error/global";
    }
}
